// 我的代言
import { PageOssMixins } from '../../../behaviors/page';
import { TARGET_TYPE } from '../../../constants/common';
import { getAdvocacyBrandList, getAdvocacyStoreList } from '../../../api/usr';

Page({
    mixins: [PageOssMixins],
    data: {
        tabsData: [{
            value: TARGET_TYPE.brand,
            title: '品牌',
        }, {
            value: TARGET_TYPE.store,
            title: '店铺',
        }],
        activeIndex: TARGET_TYPE.brand,
        searchValue: '',
        endorsementList: [],
    },
    onLoad(options) {
        this.getListData();
    },
    getListData: async function () {
        const { activeIndex, searchValue } = this.data;
        let endorsementList = [];
        const params = {
            keyword: searchValue,
        };
        if (activeIndex === TARGET_TYPE.brand) {
            endorsementList = await getAdvocacyBrandList(params);
        } else if (activeIndex === TARGET_TYPE.store) {
            endorsementList = await getAdvocacyStoreList(params);
        }
        this.setData({
            endorsementList,
        });
    },
    // 搜索用户
    searchFollow: function (event) {
        console.log('OwnerEndorsement => 搜索的内容为：', event.detail);
        this.setData({
            searchValue: event.detail,
        }, async () => {
            await this.getListData();
        });
    },
    // 切换类型
    onTabChange: function(event) {
        const { name } = event.detail;
        this.setData({
            activeIndex: name,
        }, async () => {
            await this.getListData(true);
        });
    },
})